﻿Imports System.IO
Public Class MyLibrary

    Public Shared Function toExcell(ByVal grv As DataGridView) As Boolean

        If (grv.DataSource Is Nothing) Then
            Return False
        End If

        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Microsoft.Office.Interop.Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")


        For i = 0 To grv.RowCount - 2
            For j = 0 To grv.ColumnCount - 1
                For k As Integer = 1 To grv.Columns.Count
                    xlWorkSheet.Cells(1, k) = grv.Columns(k - 1).HeaderText
                    xlWorkSheet.Cells(i + 2, j + 1) = grv(j, i).Value.ToString()
                Next
            Next
        Next

        '' Save file dialog
        Dim saveFileDialog As New SaveFileDialog()

        saveFileDialog.Filter = "Text document (*.txt)|*.txt|Excel 97-2003 Workbook (*.xls)|*.xls|Excel Workbook (*.xlsx)|*.xlsx|All files (*.*)|*.*"
        saveFileDialog.FilterIndex = 2
        saveFileDialog.RestoreDirectory = True

        If saveFileDialog.ShowDialog() = DialogResult.OK Then
            xlWorkSheet.SaveAs(saveFileDialog.FileName)
        End If


        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        Dim res As MsgBoxResult
        res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
        If (res = MsgBoxResult.Yes) Then
            Process.Start(saveFileDialog.FileName)
        End If
    End Function '' End Function toExcell

    Public Shared Function releaseObject(ByVal obj As Object) As Boolean
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Function '' End Function releaseOgject

    Public Shared Function getFATypeOwner(ByVal Module_Owner As Dictionary(Of Integer, String)) As List(Of Integer)
        Dim FAType_Owner As New List(Of Integer)

        Dim sqlWhere As String = ""
        For Each moduleID In Module_Owner
            If (moduleID.Value.Equals("F")) Then
                sqlWhere = "" & I_FAType.ColumnName_ModuleID & " = " & moduleID.Key & ""
                Dim daFAType As DataTable = New DataTable
                daFAType = New Query(New X_FAType(-1), sqlWhere).dataTable

                If (Not daFAType Is Nothing) Then
                    Dim i As Integer
                    For i = 0 To daFAType.Rows.Count - 1
                        FAType_Owner.Add(daFAType.Rows(i)(I_FAType.ColumnName_id))
                    Next

                End If
            End If
        Next

        Return FAType_Owner
    End Function

End Class
